package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.FamilyPhotoBiz;
import com.njym.java.security.app.entity.FamilyPhoto;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("familyPhoto")
public class FamilyPhotoController extends BaseController<FamilyPhotoBiz,FamilyPhoto> {

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam String familyId){
        if (!familyId.equals("undefined")) {
            List<FamilyPhoto> lastWeek = baseBiz.selectLastWeek(Integer.parseInt(familyId));
            List<FamilyPhoto> lastMonth = baseBiz.selectLastMonth(Integer.parseInt(familyId));
            List<FamilyPhoto> monthAgo = baseBiz.selectMonthAgo(Integer.parseInt(familyId));
            Map<String, Object> data = new HashMap<>(3);
            data.put("lastWeek", lastWeek);
            data.put("lastMonth", lastMonth);
            data.put("monthAgo", monthAgo);
            return new ObjectRestResponse<FamilyPhoto>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public Object addPhotos(@RequestParam String familyId, @RequestParam String addId, @RequestParam String url) {
        if (!familyId.equals("undefined") && !addId.equals("undefined")) {
            baseBiz.addPhotos(Integer.parseInt(familyId), Integer.parseInt(addId), url);
            return this.lists(familyId);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

    @RequestMapping(value = "/deletePhotos",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<?> deletePhotos(@RequestParam String list) {
        String[] arr = list.split(",");
        for (int i = 0; i < arr.length; i++) {
            baseBiz.deletePhotoById(Integer.parseInt(arr[i]));
        }
        return new ObjectRestResponse<>().rel(true);
    }
}